//
//  main.m
//  OC05_数组,字典,集合
//
//  Created by dllo on 15/12/4.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Contact.h"
#import "Book.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
#pragma mark - 知识点1 数组
        
#pragma mark * 不可变数组
        
#pragma mark * 创建对象 相关API
#if 0
        /** init方法 */
        NSArray *array = [[NSArray alloc] initWithObjects:@"beijing", @"dalian", @"hailin", nil];
        
        /** 构造器方法 */
        NSArray *array2 = [NSArray arrayWithObjects:@"beijng", @"dalian", @"hailin", nil];
        
        /** 字面量 */
        NSArray *array3 = @[@"dalian", @"beijing", @"hailin"];
#endif
#pragma mark ** 获取数组元素个数 API
#if 0
        NSArray *array = @[@"dalian", @"beijing", @"hailin"];
        NSLog(@"%ld", array.count);
#endif
#pragma mark ** 获取数组中指定下标对应的元素(对象) API
#if 0
        /** 注意: 从数组中获取对象时, 一定要明确对象类型 */
        NSArray *array = @[@"dalian", @"beijing", @"hailin"];
        NSString *city = [array objectAtIndex:1];
        NSLog(@"%@", city);
        
        /** 字面量方式获取对象 */
        NSString *name = array[1];
        NSLog(@"%@", name);
#endif
#pragma mark **  用来判断数组中是否包含某一个给定的对象
#if 0
        NSArray *array = @[@"dalian", @"haerbin", @"beijing"];
        
        if ([array containsObject:@"dalian"]) {
            NSLog(@"包含");
        } else {
            NSLog(@"不包含");
        }
#pragma mark ** 获取数组元素对应的索引
        NSArray *array1 = @[@"dalian", @"haerbin", @"beijing"];
        NSLog(@"%ld", [array indexOfObject:@"beijing"]);
#pragma mark ** 按照给定的字符串进行截取，将截取的多段字符串放入数组中
         NSString *str = @"www.kengni.com";
        
        /** 注意:这是字符串方法, 返回值是数组 */
        NSArray *arr =  [str componentsSeparatedByString:@"."];
        
        NSLog(@"arr:%@", arr);
#endif
#pragma mark ** 将数组中的元素按照给定的字符串格式拼接成一个完整的字符串对象
#if 0
        NSArray *array = @[@"beijing", @"dalian", @"hailin"];
        
        /** 注意: 这是数组的方法, 返回值是字符串 */
        NSString *str = [array componentsJoinedByString:@"&"];
        NSLog(@"%@", str);
#endif
#pragma mark ** 练习
#if 0
        /** 存在如下字符串, 将其中的图片的网址取出来
         * http://www.imanhua.com/Cover/2011-10/hyrz.jpg&http://www.imanhua.com/Cover/2011-09/op.jpg&http://www.imanhua.com/Cover/2012-04/yjdwb.jpg */
        NSString *str = @"http://www.imanhua.com/Cover/2011-10/hyrz.jpg&http://www.imanhua.com/Cover/2011-09/op.jpg&http://www.imanhua.com/Cover/2012-04/yjdwb.jpg";
        NSArray *arr = [str componentsSeparatedByString:@"&"];
        NSLog(@"%@", arr);
        
        /** 有如下一个数组，@[@“type = iOS", @"Device = iPhone", @"count = 11344115@163.com", @"password = 12345”]，将其中的内容用”&"符号拼接成一个字符串 */
        NSArray *array = @[@"type = iOS", @"Device = iPhone", @"count = 11344115@163.com", @"password = 12345"];
        NSString *str1 = [array componentsJoinedByString:@"&"];
#endif
        
        
#pragma mark -* 可变数组
#pragma mark ** 创建对象相关 API
#if 0
        NSMutableArray *mArr = [[NSMutableArray alloc] initWithObjects:@"dalian", nil];
        
        NSMutableArray *mArr2 = [NSMutableArray arrayWithCapacity:20];
        
        /** 字面量方式, 后面加上mutableCopy, copy一份可变的数组 */
        NSMutableArray *mArr3 = @[@"dalian", @"beijing", @"hailin"].mutableCopy;
#endif
#pragma mark ** 数组中添加一个对象
#if 0
        NSMutableArray *mArr = @[@"dalian", @"hailin", @"mudanjiang"].mutableCopy;
        
        /** 添加对象在数组最后 */
        [mArr addObject:@"daqing"];
        NSLog(@"%@", mArr);
#endif
#pragma mark ** 数组中指定位置插入一个对象
#if 0
        NSMutableArray *mArr = @[@"dalian", @"beijing", @"hailin"].mutableCopy;
        [mArr insertObject:@"shenyang" atIndex:2];
        NSLog(@"%@", mArr);
#endif
        
#pragma mark ** 数组中移除一个对象
#if 0
        NSMutableArray *mArr = @[@"dalian", @"beijing", @"hailin"].mutableCopy;
        [mArr removeObject:@"dalian"];
        NSLog(@"%@", mArr);
#endif
        
#pragma mark ** 移除数组中最后一个对象
#if 0
        NSMutableArray *mArr = @[@"dalian", @"beijing", @"hailin"].mutableCopy;
        [mArr removeLastObject];
        NSLog(@"%@", mArr);
#endif
#pragma mark ** 移除数组中所有的元素
#if 0
        NSMutableArray *mArr = @[@"dalian", @"beijing", @"hailin"].mutableCopy;
        [mArr removeAllObjects];
        NSLog(@"%@", mArr);
#endif
#pragma mark ** 数组中移除指定位置的元素
#if 0
        NSMutableArray *mArr = @[@"dalian", @"beijing", @"hailin"].mutableCopy;
        [mArr removeObjectAtIndex:0];
        NSLog(@"%@", mArr);
#endif
#pragma mark ** 使用指定的对象替换指定位置的对象
#if 0
        NSMutableArray *mArr = @[@"dalian", @"beijing", @"hailin"].mutableCopy;
        [mArr replaceObjectAtIndex:2 withObject:@"USA"];
        NSLog(@"%@", mArr);
#endif
        
#pragma mark ** 交换(exchange)指定的两个下标对应的对象
#if 0
        NSMutableArray *mArr = @[@"dalian", @"beijing", @"hailin"].mutableCopy;
        [mArr exchangeObjectAtIndex:0 withObjectAtIndex:1];
        NSLog(@"%@", mArr);
#endif

#pragma mark - 知识点2 字典
        
#pragma mark * 不可变字典
#pragma mark * 创建对象
#if 0
        /** init方法
         * 注意: Key 和 Value的顺序
         */
        NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:@"wangqiang", @"name", @"20", @"age", @"男", @"sex", nil];
        
        /** 构造器方法 */
        NSDictionary *dic2 = [NSDictionary dictionaryWithObjectsAndKeys:@"wangqiang", @"name", @"30", @"age", @"nv", @"sex", nil];
        
        /** 字面量写法
         * 注意顺序! key : value, key : value,
         */
        NSDictionary *dic3 = @{@"name" : @"wangqiang",
                                @"age" : @"30",
                                @"sex" : @"中"};
#endif
#pragma mark * 获取字典中键值对的个数
#if 0
        NSDictionary *dic1 = [NSDictionary dictionaryWithObjectsAndKeys:@"wangqiang", @"name", @"男", @"sex", @"50", @"age", nil];
        NSLog(@"%ld", dic1.count);
        
#pragma mark * 获取字典中所有的键
        
        NSDictionary *dic2 = [[NSDictionary alloc] initWithObjectsAndKeys:@"wangqiang", @"name", @"w", @"sex", @"80", @"age", nil];
        NSLog(@"%@", [dic2 allKeys]);
        
#pragma mark * 获取字典中所有的值
        
        NSDictionary *dic3 = @{@"name" : @"wangqiang", @"age" : @"60", @"sex": @"m"};
        NSLog(@"%@", [dic3 allValues]);
#endif
#pragma mark * 根据键获得对应的值
#if 0
        NSDictionary *dic1 = [NSDictionary dictionaryWithObjectsAndKeys:@"wangqiang", @"name", @"m", @"sex", @"60", @"age", nil];
        NSString *str1 = [dic1 objectForKey:@"age"];
        NSLog(@"%@", str1);
#endif
        
        
#pragma mark * 可变字典
        
#pragma mark * 创建对象
#if 0
        NSMutableDictionary *mDic = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"wangqiang", @"name", @"m", @"sex", @"50", @"age", nil];
        
        NSMutableDictionary *mDic2 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"wangyan", @"name", @"m", @"sex", @"80", @"age", nil];
        
        /** 字面量 */
        NSMutableDictionary *mDic3 = @{@"name" : @"wangqiang", @"age" : @"20", @"sex" : @"m"}.mutableCopy;
        
        /** 创建空字典 */
        NSMutableDictionary *mDic4 = [NSMutableDictionary dictionary];
        
        NSMutableDictionary *mDic5 = [NSMutableDictionary dictionaryWithCapacity:30];
        
#endif
        
#pragma mark * 字典中添加新的键值对以及 修改某一对键值对
#if 0
        NSMutableDictionary *mDic1 = @{@"name" : @"wangyan", @"age" : @"90", @"sex" : @"w"}.mutableCopy;
        [mDic1 setObject:@"basketball" forKey:@"hobby"];
        NSLog(@"%@", mDic1);
        
        [mDic1 setObject:@"20" forKey:@"age"];
        NSLog(@"%@", mDic1);
#endif
        
#pragma mark * 移除指定的键对应的键值对
#if 0
        NSMutableDictionary *mDic1 = @{@"name" : @"wangqiang", @"sex" : @"m", @"age" : @"20"}.mutableCopy;
        [mDic1 removeObjectForKey:@"age"];
        NSLog(@"%@", mDic1);
        
#pragma mark * 移除字典中所有的键值对
        
        NSMutableDictionary *mDic2 = @{@"name" : @"wangyan", @"sex" : @"m", @"age" : @"30"}.mutableCopy;
        [mDic2 removeAllObjects];
#endif
        
#pragma mark * 练习
#if 0
        NSMutableDictionary *mDic = @{@"A":@[],
                                      @"B":@[],
                                      @"C":@[],
                                      @"D":@[],
                                      @"E":@[],
                                      @"F":@[],
                                      @"G":@[],
                                      @"H":@[],
                                      @"I":@[],
                                      @"J":@[],
                                      @"K":@[],
                                      @"L":@[],
                                      @"M":@[],
                                      @"N":@[],
                                      @"O":@[],
                                      @"P":@[],
                                      @"Q":@[],
                                      @"R":@[],
                                      @"S":@[],
                                      @"T":@[],
                                      @"U":@[],
                                      @"V":@[],
                                      @"W":@[],
                                      @"X":@[],
                                      @"Y":@[],
                                      @"Z":@[]}.mutableCopy;
#endif
#if 0

        NSMutableDictionary *mDic = [NSMutableDictionary dictionaryWithCapacity:26];
        
        /** for循环, 循环26次, 添加键值对 */
        for (int i = 0; i < 26; i++) {
        
            /** 创建Key值 */
            NSString *key = [NSString stringWithFormat:@"%c", 'A' + i];
            
            /** 创建空的可变数组 */
            NSMutableArray *mArr = [NSMutableArray array];
            
            /** 字典中添加新的键值对 */
            [mDic setObject:mArr forKey:key];
        }
        
        
        
        
        
        
        /** 2.创建联系人对象 */
        Contact *con1 = [[Contact alloc] initWithName:@"Zhangsan" phoneNumber:@"13855555555"];
        Contact *con2 = [[Contact alloc] initWithName:@"Wangqiang" phoneNumber:@"11233333333"];
        Contact *con3 = [[Contact alloc] initWithName:@"Heiliu" phoneNumber:@"13945308106"];
        
        
        /** 3.根据联系人分组, 在字典中找出分组对应的数组 */
        NSString *key = con1.group;
        NSMutableArray *mArr = [mDic objectForKey:key];
        
        /** 4. 将联系人对象添加到数组中 */
        [mArr addObject:con1];
        [[mDic objectForKey:con2.group] addObject:con2];
        [[mDic objectForKey:con3.group] addObject:con3];
        
        for (NSString *key in mDic) {
            NSArray *arr = [mDic objectForKey:key];
            for (Contact *contact in arr) {
                [contact information];
            }
        }
#endif
      
#pragma mark - 知识点3 快速遍历容器(数组, 字典, 集合)
#if 0
        NSArray *arr = @[@"wangqiang", @"wangyan", @"heiliu"];
        
        /** forin 快速遍历数组 */
        for (NSString *str in arr) {
            NSLog(@"%@", str);
        }
        
        Contact *con1 = [[Contact alloc] initWithName:@"Zhangsan" phoneNumber:@"13855555555"];
        Contact *con2 = [[Contact alloc] initWithName:@"Wangqiang" phoneNumber:@"13552529498"];
        Contact *con3 = [[Contact alloc] initWithName:@"Heiliu" phoneNumber:@"13945308106"];
        
        NSArray *conArr = @[con1, con2, con3];
        for (Contact *con in conArr) {
            [con information];
        }
#endif
#if 0
        /** forin 快速遍历字典 */
        NSDictionary *dic = @{@"zhangsan" : @"name", @"age" : @"25", @"sex" : @"m", @"hobby" : @"basketball"};
        
        /** 注意: 对字典快速遍历, 遍历的是Key */
        for (NSString *key in dic) {
            NSLog(@"%@", [dic objectForKey:key]);
        }
#endif
        
#pragma mark - 知识点4 集合
        
#pragma mark * 不可变集合
#pragma mark ** 创建对象 API
#if 0
        /** 注意: 集合的互异性, 不能存在相同的对象 */
        NSSet *set = [[NSSet alloc] initWithObjects:@"gaoshou", @"wangyan", @"heiliu", @"wangqiang", nil];
        
        NSSet *set2 = [NSSet setWithObjects:@"zhang", @"liu", @"hei", @"wang", nil];
        
#pragma mark ** 个数
        NSLog(@"%ld", set.count);
        
#pragma mark ** 取出对象
        NSLog(@"%@", [set2 anyObject]);

#pragma mark ** 取出所有对象
        NSArray *arr = [set allObjects];
        NSLog(@"%@", arr);
        
#pragma mark ** 判断是否含有某一个对象
        if ([set containsObject:@"jiao"]) {
            NSLog(@"含有");
        } else {
            NSLog(@"不含有");
        }
#endif
#pragma mark * 可变集合
#pragma mark ** 创建对象
#if 0
        NSMutableSet *mSet = [[NSMutableSet alloc] initWithObjects:@"zhang", @"wang", @"hao", nil];
        
        NSMutableSet *mSet2 = [NSMutableSet setWithObjects:@"zhang", @"li", @"zhao", nil];
        
#pragma mark ** 添加对象
        [mSet addObject:@"ni"];
        
#pragma mark ** 删除对象
        [mSet removeObject:@"wang"];
        
#pragma mark ** 移除所有
        [mSet2 removeAllObjects];
        
#pragma mark ** 快速遍历集合
        for (NSString *str in mSet) {
            NSLog(@"%@", str);
        }
#endif
        
        
#pragma mark - 作业
        
        /** 创建书籍对象 */
        Book *book1 = [[Book alloc] initWithName:@"aShuai" price:@"10.5"];
        Book *book2 = [[Book alloc] initWithName:@"douPo" price:@"55"];
        Book *book3 = [[Book alloc] initWithName:@"wuDong" price:@"66"];
        
        NSMutableArray *mArr = @[book1, book2].mutableCopy;
        
        for (Book *book in mArr) {
            [book information];
        }
        
        [mArr addObject:@[book3]];
        
        
        

        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
    }
    return 0;
}
